/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>

const bit<16> TYPE_IPV4 = 0x800;
const bit<16> TYPE_ARP = 0x0806;
const bit<16> ARP_REPLY = 2;

/*************************************************************************
*********************** H E A D E R S  ***********************************
*************************************************************************/

typedef bit<9>  egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
typedef bit<48>  mac_addr_t;
typedef bit<32>  ipv4_addr_t;
typedef bit<9>   port_id_t;

header ethernet_t {
    macAddr_t dstAddr;
    macAddr_t srcAddr;
    bit<16>   etherType;
}

header ipv4_t {
    bit<4>    version;
    bit<4>    ihl;
    bit<8>    diffserv;
    bit<16>   totalLen;
    bit<16>   identification;
    bit<3>    flags;
    bit<13>   fragOffset;
    bit<8>    ttl;
    bit<8>    protocol;
    bit<16>   hdrChecksum;
    ip4Addr_t srcAddr;
    ip4Addr_t dstAddr;
}

// 定义了一些常量和数据结构，用于处理ARP（地址解析协议）相关的操作
const bit<16> ARP_HTYPE_ETHERNET = 0x0001;
const bit<16> ARP_PTYPE_IPV4     = 0x0800;
const bit<8>  ARP_HLEN_ETHERNET  = 6;
const bit<8>  ARP_PLEN_IPV4      = 4;
const bit<16> ARP_OPER_REQUEST   = 1;
const bit<16> ARP_OPER_REPLY     = 2;

header arp_t {
    bit<16> htype;
    bit<16> ptype;
    bit<8>  hlen;
    bit<8>  plen;
    bit<16> oper;
}

header arp_ipv4_t {
    mac_addr_t  sha;
    ipv4_addr_t spa;
    mac_addr_t  tha;
    ipv4_addr_t tpa;
}

struct metadata {
    /* empty */

}
// 将定义好的数据结构添加到headers当中
struct headers {
    ethernet_t   ethernet;
    ipv4_t       ipv4;
    arp_t       arp;
    arp_ipv4_t arp_ipv4;
}


/*************************************************************************
*********************** P A R S E R  ***********************************
*************************************************************************/

parser MyParser(packet_in packet,
                out headers hdr,
                inout metadata meta,
                inout standard_metadata_t standard_metadata) {

    state start {
        transition parse_ethernet;
    }

 
    state parse_ethernet {
        packet.extract(hdr.ethernet);
        transition select(hdr.ethernet.etherType) {
            TYPE_IPV4: parse_ipv4;
            TYPE_ARP: parse_arp;
            default: accept;
        }
    }

    // 解析ARP头部的状态
    state parse_arp {
        // 提取头部信息
        packet.extract(hdr.arp);
        // 根据ARP头部信息选择下一个状态
        transition select(hdr.arp.htype, hdr.arp.ptype, hdr.arp.hlen, hdr.arp.plen) {
            (ARP_HTYPE_ETHERNET, ARP_PTYPE_IPV4, ARP_HLEN_ETHERNET,  ARP_PLEN_IPV4): parse_arp_ipv4;
            default : accept;

        }
    }

    // 解析ARP IPv4状态
    state parse_arp_ipv4 {
        packet.extract(hdr.arp_ipv4);
        //meta.dst_ipv4 = hdr.arp_ipv4.tpa;
        transition accept;
    }

    state parse_ipv4 {
        packet.extract(hdr.ipv4);
        //meta.dst_ipv4 = hdr.ipv4.dstAddr;
        transition accept;
    }
    
    state parse_arp_reply {
        packet.extract(hdr.arp_ipv4);
       // meta.dst_ipv4 = hdr.arp_ipv4.tpa;
        transition accept;
    }


}

/*************************************************************************
************   C H E C K S U M    V E R I F I C A T I O N   *************
*************************************************************************/

control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
    apply {  }
}


/*************************************************************************
**************  I N G R E S S   P R O C E S S I N G   *******************
*************************************************************************/

control MyIngress(inout headers hdr,
                  inout metadata meta,
                  inout standard_metadata_t standard_metadata) {
    action drop() {
        mark_to_drop(standard_metadata);
    }


    action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
        standard_metadata.egress_spec = port;
        hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
        hdr.ethernet.dstAddr = dstAddr;
        hdr.ipv4.ttl = hdr.ipv4.ttl - 1;

        //meta.mac_da      = dstAddr;
       // meta.mac_sa      =  hdr.ethernet.dstAddr;
        //meta.egress_port = port;
    }

    table ipv4_lpm {
        key = {
            hdr.ipv4.dstAddr: lpm;
        }
        actions = {
            ipv4_forward;
            drop;
            NoAction;
        }
        size = 1024;
        default_action = drop();
    }

/*
    action send_arp_reply(mac_addr_t macAddr) {
        hdr.ethernet.dstAddr = hdr.arp_ipv4.sha;
        hdr.ethernet.srcAddr = macAddr;

        hdr.arp.oper         = ARP_OPER_REPLY;

        hdr.arp_ipv4.tha     = hdr.arp_ipv4.sha;
        hdr.arp_ipv4.tpa     = hdr.arp_ipv4.spa;
        hdr.arp_ipv4.sha     = macAddr;
        hdr.arp_ipv4.spa     = meta.dst_ipv4;

        standard_metadata.egress_spec = standard_metadata.ingress_port;

    }

    table arp_request {
        key = {
            hdr.arp.isValid() : exact;
            hdr.arp.oper : exact;
        }
        actions = {
            send_arp_reply;
        }
        size = 1024;
    }

    table ipv4_request {
        key = {
            hdr.arp_ipv4.isValid() : exact;
            hdr.ipv4.isValid() : exact;
        }
        actions = {
            ipv4_forward;
        }
        size = 1024;
    }
*/

     action send_arp_reply(mac_addr_t macAddr) {
        
        // 将以太网头部的目标MAC地址设置为ARP头部的源MAC地址。
        hdr.ethernet.dstAddr = hdr.arp_ipv4.sha;
        // 将以太网头部的源MAC地址设置为传入的mac地址
        hdr.ethernet.srcAddr = macAddr;
        // 设置ARP操作为回复
        hdr.arp.oper         = ARP_OPER_REPLY;
        // 修改arp_ipv4头部信息,定义temp变量用于交换目标物理地址和源物理地址
        ip4Addr_t temp = hdr.arp_ipv4.tpa;
        hdr.arp_ipv4.tha     = hdr.arp_ipv4.sha;
        hdr.arp_ipv4.tpa     = hdr.arp_ipv4.spa;
        hdr.arp_ipv4.sha     = macAddr;
        hdr.arp_ipv4.spa     = temp;

        standard_metadata.egress_spec = standard_metadata.ingress_port;
    }

    table arp_ipv4 {
        key = {
            hdr.arp_ipv4.tpa: exact;
        }
        actions = {
            send_arp_reply;
            NoAction;
        }
        size = 1024;
        default_action = NoAction();
    }



    apply {
    	/*
        meta.my_mac = 0x000102030405;
        ipv4_lpm.apply();
        if (hdr.arp.isValid() && hdr.arp.oper == ARP_OPER_REQUEST) {
            arp_request.apply();
        } else if (!hdr.arp.isValid() && hdr.arp_ipv4.isValid() && hdr.ipv4.isValid()) {
            ipv4_request.apply();
        } else {
            drop();
        }
        */

        if (hdr.ipv4.isValid()) {
            ipv4_lpm.apply();
        }
        // 检查ARP IPv4头部是否有效且ARP操作为请求
        if (hdr.arp_ipv4.isValid() && hdr.arp.oper == ARP_OPER_REQUEST) {
            arp_request.apply();
        }


    }
}

/*************************************************************************
****************  E G R E S S   P R O C E S S I N G   *******************
*************************************************************************/

control MyEgress(inout headers hdr,
                 inout metadata meta,
                 inout standard_metadata_t standard_metadata) {
    apply {  }
}

/*************************************************************************
*************   C H E C K S U M    C O M P U T A T I O N   **************
*************************************************************************/

control MyComputeChecksum(inout headers  hdr, inout metadata meta) {
     apply {
        update_checksum(
        hdr.ipv4.isValid(),
            { hdr.ipv4.version,
              hdr.ipv4.ihl,
              hdr.ipv4.diffserv,
              hdr.ipv4.totalLen,
              hdr.ipv4.identification,
              hdr.ipv4.flags,
              hdr.ipv4.fragOffset,
              hdr.ipv4.ttl,
              hdr.ipv4.protocol,
              hdr.ipv4.srcAddr,
              hdr.ipv4.dstAddr },
            hdr.ipv4.hdrChecksum,
            HashAlgorithm.csum16);
    }
}

/*************************************************************************
***********************  D E P A R S E R  *******************************
*************************************************************************/

control MyDeparser(packet_out packet, in headers hdr) {
    apply {
        packet.emit(hdr.ethernet);
        packet.emit(hdr.ipv4);
        packet.emit(hdr.arp);
        packet.emit(hdr.arp_ipv4);
    }
}

/*************************************************************************
***********************  S W I T C H  *******************************
*************************************************************************/

V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;